package com.sdkteam.Ailatrieuphu;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.widget.Toast;

public class SMSSender {

	public static void sendSMS(String phoneNumber, String message,
			final Activity act) {
		String SENT = "SMS_SENT";
		String DELIVERED = "SMS_DELIVERED";

		// System.out.println("SNED=" + message + "|to|" + phoneNumber);

		PendingIntent sentPI = PendingIntent.getBroadcast(act.getBaseContext(),
				0, new Intent(SENT), 0);

		PendingIntent deliveredPI = PendingIntent.getBroadcast(
				act.getBaseContext(), 0, new Intent(DELIVERED), 0);

		final BroadcastReceiver br = new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(act.getBaseContext(), "Tin nhắn đã gửi.",
							Toast.LENGTH_SHORT).show();
					// update
					break;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Toast.makeText(act.getBaseContext(), "Lỗi gửi tin.",
							Toast.LENGTH_SHORT).show();

					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Toast.makeText(act.getBaseContext(), "Không có dịch vụ.",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Toast.makeText(act.getBaseContext(), "Lỗi PDU",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Toast.makeText(act.getBaseContext(), "Mất sóng.",
							Toast.LENGTH_SHORT).show();
					break;
				}
				act.getBaseContext().unregisterReceiver(this);
			}
		};
		act.getBaseContext().registerReceiver(br, new IntentFilter(SENT));

		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

	}

}
